package me.xplabs.engine.display 
{
	import flash.display.BitmapData;
	import flash.utils.getTimer;
	import me.xplabs.engine.display.BaseDisplay;
	
	/**
	 * 位图动画基类
	 * ...
	 * @author xiaohan
	 */
	public class BaseAnimate extends BaseDisplayContainer 
	//public class BaseAnimate extends BaseDisplay 
	{
		public var sequence:Array;
		public var gap:int;
		public var sequenceIndex:int;
		protected var _prev:uint;
		protected var _now:uint;
		protected var _totalTime:uint;
		protected var _speed:int;
		protected var _repeat:Boolean;
		
		public var playOver:Function;
		public var frameInt:int;
		private var _bitmapData:*;
		public function BaseAnimate() 
		{
			super();
			sequenceIndex = 0;
		}
		public function setAnimate(p_speed:int, p_repeat:Boolean = true, p_frameInt:int = 12):void
		{
			_speed = p_speed;
			_repeat = p_repeat;
			frameInt = p_frameInt;
		}
		override public function update(container:BitmapData):void 
		{
			_prev = getTimer() - _now;
			_now = getTimer();
			_totalTime += _prev;
			gap = _totalTime / _speed;
			if (gap >= 1)
			{
				_totalTime = _totalTime-gap * _speed;
				sequenceIndex += gap;
				if (sequenceIndex >= frameInt)
				{
					if (!_repeat)
					{
						sequenceIndex = -1;
						if (playOver != null) playOver(this);
					}else
					{
						sequenceIndex = 0;
					}
				}
				if (!sequence || sequenceIndex > sequence.length - 1 || sequenceIndex < 0)
				{
					data = null;
				}else
				{
					data = sequence[sequenceIndex];
				}
			}
			if (data)
			{
				if (data.hasOwnProperty("x") && data.hasOwnProperty("y"))
				{
					x = x - data.x;
					y = y - data.y;
				}else
				{
					x = x - data.width * .5;
					y = y - data.height * .5;
				}
			}
			super.update(container);
		}
		override public function dispose():void 
		{
			sequence = null;
			sequenceIndex = 0;
			_speed = 0;
			_repeat = false;
			frameInt = 0;
			playOver = null;
			_now = 0;
			_prev = 0;
			_totalTime = 0;
			gap = 0;
			super.dispose();
		}
	}
}